package com.aynu.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author mj
 * @version 1.0
 * @date 2020/6/25 8:17
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set = new HashSet<>();
    //初始化方法
    @Override
    public void initialize(ListValue constraintAnnotation) {
        //获取注解属性vals的值
        int[] vals = constraintAnnotation.vals();
        //将vals放入set
        for (int val:vals)
        {
            set.add(val);
        }

    }

    /**
     *
     * @param integer 是被标注注解的字段的值，如@ListValue(vals = {0,1})
     * 	                                    private Integer showStatus;  的showStatus的值
     * @param constraintValidatorContext
     * @return
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        //set.contains :set里面是否包含integer，返回是一个Boolean值
        return set.contains(integer);
    }
}
